package com.wanchuang.qhbb.module.login;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;


import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.Connect;
import com.wanchuang.qhbb.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity {


    @BindView(R.id.phone_et)
    EditText phoneEt;  // 手机输入框
    @BindView(R.id.get_code_tv)
    TextView getCodeTv;   // 获取验证码
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.mobile_code_et)
    EditText mobileCodeEt;  // 验证码
    @BindView(R.id.password_et)
    EditText passwordEt;  // 密码输入框
    @BindView(R.id.confirm_password_et)
    EditText confirmPasswordEt;  // 确认输入框
    @BindView(R.id.login_cb_password)
    CheckBox loginCbPassword;  // 密码显示检查框
    @BindView(R.id.login_cb_confirm_password)
    CheckBox loginCbConfirmPassword;  // 确认密码显示检查框

    private String mPhone;  // 电话号码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        StatusBarUtil.setWhiteStatusBar(this);
        ButterKnife.bind(this);
        setPasswordCanSee(loginCbPassword,passwordEt);
        setPasswordCanSee(loginCbConfirmPassword,confirmPasswordEt);
        tv_title.setText("忘记密码");
        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String s = charSequence.toString();
                if (s.length() == 11) {
                    if (mPhone != null && (!mPhone.equals(s))) {
                        mobileCodeEt.setText("");
                        passwordEt.setText("");
                        confirmPasswordEt.setText("");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    /**
     * 点击注册按钮
     */
    private void submit() {
        mPhone = phoneEt.getText().toString();
        if (TextUtils.isEmpty(mPhone)) {
            showToast("请输入手机号");
            return;
        }

        if (mPhone.length() != 11) {
            mPhone = null;
            showToast("请输入11位手机号码");
            return;
        }
        String code = mobileCodeEt.getText().toString();
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }
        String pwd = passwordEt.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            showToast("请设置8-16位数字与字母组合的登录密码");
            return;
        }
        String confirmPwd = confirmPasswordEt.getText().toString();
        if (!pwd.equals(confirmPwd)) {
            showToast("两次设置的登录密码不一致");
            return;
        }
        showProgress();
        Connect.sms_register.Builder builder = Connect.sms_register.newBuilder();
        builder.setPhone(mPhone).setPassword(pwd).setCaptcha(code).setRePassword(confirmPwd);
        NetworkManager.INSTANCE.post(Apis.forgetPassword,builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {
                hideProgress();
            }

            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                if (code == 200) {
                    showToast("修改成功！");
                    finish();
                } else {
                    showToast(errorMessage);
                }
            }
        });
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        mPhone = phoneEt.getText().toString();
        if (TextUtils.isEmpty(mPhone)) {
            showToast("请输入手机号码");
            return;
        }
        if (mPhone.length() != 11) {
            mPhone = null;
            showToast("请输入11位手机号码");
            return;
        }
        Connect.captcha.Builder builder = Connect.captcha.newBuilder();
        builder.setPhone(mPhone);
        builder.setType("3");
        Connect.captcha data = builder.build();
        showProgress();
        NetworkManager.INSTANCE.post(Apis.getMobileCode, data.toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {
                hideProgress();
            }

            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                showToast(errorMessage);
                if (code == 200) {
                    countDownReSend(getCodeTv, 60);
                }
            }
        });
    }

    /**
     * 设置密码是否为明文显示
     */
    private void setPasswordCanSee(CheckBox checkBox,EditText editText) {
        checkBox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                //设置为密文显示
                editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                //设置为明文显示
                editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
            //光标在最后显示
            editText.setSelection(editText.length());
        });
    }

    @OnClick({R.id.back_iv, R.id.get_code_tv, R.id.forget_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.get_code_tv:
                getCode();
                break;
            case R.id.forget_btn:
                submit();
                break;
        }
    }

}

